package com.bjsc.controller.data;

import com.baomidou.mybatisplus.plugins.Page;
import com.bjsc.service.IRechargeService;
import com.bjsc.common.bean.Rest;
import com.bjsc.common.controller.SuperController;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.Map;

/**
 * @author create by VIC.Huang
 * @date 2018/2/8
 */
@Controller
@RequestMapping("/data/recharge")
public class RechargeController extends SuperController {
    @Autowired
    private IRechargeService rechargeService;

    @RequiresPermissions("listRecharge")
    @RequestMapping("/list/{pageNumber}")
    public String list(@PathVariable Integer pageNumber, @RequestParam(defaultValue = "10") Integer pageSize, Model model) {
        Page<Map<Object, Object>> page = getPage(pageNumber, pageSize);
        model.addAttribute("pageSize", pageSize);
        Page<Map<Object, Object>> pageData = rechargeService.selectRechargePage(page);
        model.addAttribute("pageData", pageData);
        return "data/recharge/list";
    }

    @RequiresPermissions("manRecharge")
    @RequestMapping("/recharge")
    @ResponseBody
    public Rest recharge(@RequestParam("userId")Long userId, @RequestParam("money")BigDecimal money) {
        try {
            if(rechargeService.recharge(userId, money)) {
                return Rest.ok();
            }else {
                return Rest.failure("充值失败！");
            }
        } catch (Exception e) {
            return Rest.failure("充值失败！ERROR: " + e.getMessage());
        }
    }

    @RequiresPermissions("manRecharge")
    @RequestMapping("/rechargeIndex")
    public String rechargeIndex() {
        return "data/recharge/add";
    }
}
